﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 记事本2._0
{
    
    public partial class Form1 : Form
    {
        //注意设置的私有成员名称，后面将会用到，名字可以自己设定
        private OpenFileDialog od = new OpenFileDialog();
        private SaveFileDialog sd = new SaveFileDialog();
        private bool richboxTextHasChanged = false;
       
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            od.FileName = "";
            od.Filter = "TXT FILE(*.txt)|*.txt";
            sd.Filter = "TXT FILE(*.txt)|*.txt";
            label2.Text = "时间：" + DateTime.Now.ToLongTimeString().ToString();
            timer1.Enabled = true;
            timer1.Interval = 100;
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (od.FileName != "" && richboxTextHasChanged == true && MessageBox.Show("文本内容已更改\n是否保存修改？", "信息提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {//若文本改动，首先保存
                richTextBox1.SaveFile(od.FileName, RichTextBoxStreamType.PlainText);
            }
            //不然初始化界面
            od.FileName = "";
            this.Text = "记事本";
            this.richTextBox1.Clear();
            this.richboxTextHasChanged = false;
        }

        private void 打开OCtrlOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            od.ShowDialog();//选择打开文本文件
            if (od.FileName != "")
            {
                //将文本文件里面的内容加载到界面内
                richTextBox1.LoadFile(od.FileName, RichTextBoxStreamType.PlainText);
                this.Text = od.FileName + "-记事本";
            }
        }

        private void 保存SCtrlSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            od.ShowDialog();//选择打开文本文件
            if (od.FileName != "")
            {
                //将文本文件里面的内容加载到界面内
                richTextBox1.LoadFile(od.FileName, RichTextBoxStreamType.PlainText);
                this.Text = od.FileName + "-记事本";
            }
        }

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                richTextBox1.SaveFile(sd.FileName, RichTextBoxStreamType.PlainText);
            }
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void 复制CCtrlCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void 粘贴PCtrlVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //设置字体
            FontDialog fd = new FontDialog();
            fd.ShowDialog();
            richTextBox1.Font = fd.Font;
        }

        private void 全选ACtrlAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void 时间日期DF5ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText(System.DateTime.Now.ToString());
        }

        private void 查看帮助HToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://answers.microsoft.com/en-us/windows/forum/apps_windows_10");
        }

        private void 关于记事本AToolStripMenuItem_Click(object sender, EventArgs e)
        {
          
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //创建对象
            Graphics myGra = this.CreateGraphics();
            //画直线
            Pen myPen1 = new Pen(Color.Red, 2);
            myGra.DrawLine(myPen1, 100, 0, 300, 500);
            //画圆及椭圆
            Pen myPen2 = new Pen(Color.Orange, 2);
            myGra.DrawEllipse(myPen2, 80, 80, 50, 50);
            myGra.DrawEllipse(myPen2, 200, 100, 60, 120);
            //画矩形
           Pen myPen3 = new Pen(Color.Yellow, 3);
           myGra.DrawRectangle(myPen3, 123, 234, 60, 60);//正方形
           myGra.DrawRectangle(myPen3,23, 34, 60, 20);//任意矩形
           
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics clearG = this.CreateGraphics();
            clearG.Clear(Color.White);
        }
       
        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void 二进制文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "bin 文件|*.bin";
            sf.AddExtension = true;
            sf.Title = "写二进制文件";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(sf.FileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(richTextBox1.Text);
                richTextBox1.Text = "";
                bw.Close();
                fs.Close();
            }
        }


        private void 二进制读取RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "bin 文件|*.bin";
            sf.AddExtension = true;
            sf.Title = "写二进制文件";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(sf.FileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(richTextBox1.Text);
                richTextBox1.Text = "";
                bw.Close();
                fs.Close();
            }
        }
    }
}